package com.jetpackframework.retrofit.converter;

import android.text.TextUtils;
import android.widget.TextView;

import com.gwm.annotation.http.HTTP;
import com.gwm.annotation.http.Header;
import com.gwm.annotation.http.HeaderMap;
import com.gwm.annotation.http.HeaderString;
import com.gwm.annotation.http.Path;
import com.gwm.annotation.http.QueryUrl;
import com.gwm.annotation.http.Url;
import com.gwm.annotation.retrofit.HttpModel;
import com.jetpackframework.applicationdelegate.BaseApplication;
import com.jetpackframework.http.HttpUtil;
import com.jetpackframework.retrofit.Converter;
import com.tencent.mmkv.MMKV;
import com.zhy.http.okhttp.builder.OkHttpRequestBuilder;

import java.io.File;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.Map;
import java.util.Set;

import okhttp3.Request;

public abstract class HttpRequestConverter<T> implements Converter<T> {
    public static final String BASE_URL = "baseUrl";
    protected void initHttpBasicInfo(Method method,Object[] args, OkHttpRequestBuilder builder) {
        Header header = method.getAnnotation(Header.class);
        String url = null;

        HTTP annotation = method.getAnnotation(HTTP.class);
        if(method.isAnnotationPresent(HTTP.class)) {
            url = annotation.url();
        }
        String baseUrl = method.getDeclaringClass().getAnnotation(HttpModel.class).baseUrl();
        if (TextUtils.isEmpty(baseUrl)){
            baseUrl = MMKV.defaultMMKV().getString(BASE_URL,null);
        }
        if (!TextUtils.isEmpty(baseUrl)) {
            if (!url.startsWith("http")) {
                url = baseUrl + url;
            }
        }
        if (header != null){
            String[] strings = header.value();
            for (String string : strings){
                String keyvalue[] = string.split(":");
                builder.addHeader(keyvalue[0],keyvalue[1]);
            }
        }
        builder.url(url);
        Annotation[][] annotations = method.getParameterAnnotations();
        for (int i = 0; i < annotations.length; i++) {     //i:第几个参数的注解
            if (annotations[i].length > 0) {
                for (int j = 0; j < annotations[i].length; j++) {  //j: 第i个参数上的第j个注解
                    if (annotations[i][j] instanceof QueryUrl){
                        String key = ((QueryUrl)annotations[i][j]).value();
                        Object value = args[i];
                        if (url != null){
                            if (url.contains("?")){
                                url += ("&&" + key + "=" + value);
                            }else {
                                url += ("?" + key + "=" + value);
                            }
                        }
                        builder.url(url);
                    }else if(annotations[i][j] instanceof Url){
                        if(args[i] instanceof String){
                            url = (String) args[i];
                        }
                        builder.url(url);
                    }else if (annotations[i][j] instanceof Path){
                        String value = ((Path)annotations[i][j]).value();
                        url = url.replaceAll("\\{"+value+"\\}", args[i]+"");
                        builder.url(url);
                    }else if (annotations[i][j] instanceof HeaderString){
                        String key = ((HeaderString)annotations[i][j]).value();
                        String value = (String) args[i];
                        builder.addHeader(key,value);
                    }else if (annotations[i][j] instanceof HeaderMap){
                        Map<String,String> paramMap = (Map<String,String>) args[i];
                        Set<String> keySet = paramMap.keySet();
                        for (String key : keySet){
                            String value = paramMap.get(key);
                            builder.addHeader(key, value);
                        }
                    }
                }
            }
        }
    }
    protected void initHttpBasicInfo(Method method,Object[] args, Request.Builder builder) {
        Header header = method.getAnnotation(Header.class);
        String url = null;

        HTTP annotation = method.getAnnotation(HTTP.class);
        if(method.isAnnotationPresent(HTTP.class)) {
            url = annotation.url();
        }
        String baseUrl = method.getDeclaringClass().getAnnotation(HttpModel.class).baseUrl();
        if (TextUtils.isEmpty(baseUrl)){
            baseUrl = MMKV.defaultMMKV().getString(BASE_URL,null);
        }
        if (!TextUtils.isEmpty(baseUrl)) {
            if (!url.startsWith("http")) {
                url = baseUrl + url;
            }
        }
        if (header != null){
            String[] strings = header.value();
            for (String string : strings){
                String keyvalue[] = string.split(":");
                builder.addHeader(keyvalue[0],keyvalue[1]);
            }
        }
        builder.url(url);
        Annotation[][] annotations = method.getParameterAnnotations();
        for (int i = 0; i < annotations.length; i++) {     //i:第几个参数的注解
            if (annotations[i].length > 0) {
                for (int j = 0; j < annotations[i].length; j++) {  //j: 第i个参数上的第j个注解
                    if (annotations[i][j] instanceof QueryUrl){
                        String key = ((QueryUrl)annotations[i][j]).value();
                        Object value = args[i];
                        if (url != null){
                            if (url.contains("?")){
                                url += ("&&" + key + "=" + value);
                            }else {
                                url += ("?" + key + "=" + value);
                            }
                            builder.url(url);
                        }
                    }else if (annotations[i][j] instanceof Path){
                        String value = ((Path)annotations[i][j]).value();
                        url = url.replaceAll("\\{"+value+"\\}", args[i]+"");
                        builder.url(url);
                    }else if (annotations[i][j] instanceof HeaderString){
                        String key = ((HeaderString)annotations[i][j]).value();
                        String value = (String) args[i];
                        builder.addHeader(key,value);
                    }else if (annotations[i][j] instanceof HeaderMap){
                        Map<String,String> paramMap = (Map<String,String>) args[i];
                        Set<String> keySet = paramMap.keySet();
                        for (String key : keySet){
                            String value = paramMap.get(key);
                            builder.addHeader(key, value);
                        }
                    }
                }
            }
        }
    }
}
